package com.pdg.android.guessmynumber;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class GameData {
	private static final int NUMBER_MINIMUM = 1;
	private static final int NUMBER_MAXIMUM = 100;
	
	private static int number;
	private static List<GuessRecord> history = new ArrayList<GuessRecord>();

	public static void startGame() {
		pickNumber();
		clearHistory();
	}

	private static void clearHistory() {
		getHistory().clear();
	}

	private static void pickNumber() {
		setNumber(Utilities.generateRandomNumber(NUMBER_MINIMUM,NUMBER_MAXIMUM));
	}

	private static void setNumber(int theNumber) {
		number = theNumber;
	}
	
	public static int getNumber(){
		return number;
	}
	
	public static boolean isGameOver(){
		if(history.size()>0){
			return getLastGuess().getState()==GuessState.CORRECT;
		}else{
			return false;
		}
	}
	public static GuessRecord getLastGuess(){
		if(getHistory().size()>0){
			return getHistory().get(getHistory().size()-1);
		}else{
			return null;
		}
	}
	public static void addGuess(Integer theGuess){
		GuessState theState;
		if(theGuess==null){
			theState = GuessState.INVALID;
		}else if(theGuess>=NUMBER_MINIMUM && theGuess<=NUMBER_MAXIMUM){
			if(theGuess>getNumber()){
				theState = GuessState.TOO_HIGH;
			}else if(theGuess<getNumber()){
				theState = GuessState.TOO_LOW;
			}else{
				theState = GuessState.CORRECT;
			}
		}else{
			theState = GuessState.OUT_OF_RANGE;
		}
		getHistory().add(new GuessRecord(theGuess,theState));
	}
	public static Iterator<GuessRecord> getHistoryIterator(){
		return getHistory().iterator();
	}
	private static List<GuessRecord> getHistory() {
		return history;
	}
	public static int getGuessCount(){
		return getHistory().size();
	}
}
